import * as THREE from "three";
import { RGBELoader } from "three/examples/jsm/loaders/RGBELoader";
// 导入控制器，轨道控制器
import { OrbitControls } from "three/examples/jsm/controls/OrbitControls";
// 导入模型解析器
import { GLTFLoader } from "three/examples/jsm/loaders/GLTFLoader";

import { OBJLoader } from "three/examples/jsm/loaders/OBJLoader";
class Base3d {
  constructor(selector) {
    this.container = document.querySelector(selector);
    this.camera;
    this.scene;
    this.renderer;
    this.model;
    this.panzi;
    this.light;
    this.ambientLight;
    // this.animateAction;
    this.clock = new THREE.Clock();
    // this.onFinish = onFinish;
    this.init();
    this.animate();
    this.mixer
    // this.progressFn;
  }
  //声明:-----------
  init() {
    //   初始化场景
    this.initScene();
    // 初始化相机
    this.initCamera();
    // 初始化渲染器
    this.initRenderer();
    //设置控制器:用户自身行为  关闭即可
    this.initControls();
    //添加物体的函数:  添加玻璃盘等
    this.addMesh()
    //监听窗口的辩护 自动更新变化:
    window.addEventListener("resize",this.onWindowResize.bind(this))
    // 监听滚轮事件
    //window.addEventListener("mousewheel", this.onMouseWheel.bind(this));
  }
  //定义:-----------
  //定义场景:
  initScene() {
    this.scene = new THREE.Scene();
    this.setEnvMap("005")
  }
  //定义相机 为透视相机:
  initCamera() {
    this.camera = new THREE.PerspectiveCamera(
      45,
      window.innerWidth / window.innerHeight,
      .1,
      1000
    );
    this.camera.position.set(1,2,2);
  }
  //定义渲染器:
  initRenderer() {
    this.renderer = new THREE.WebGLRenderer({ antialias: true });
    // 设置屏幕像素比
    this.renderer.setPixelRatio(window.devicePixelRatio);
    // 渲染的尺寸大小
    this.renderer.setSize(window.innerWidth,window.innerHeight);
    //设置线性映射会暗一点 或 电影级别会光亮一点
    this.renderer.toneMapping = THREE.ACESFilmicToneMapping
    //设置渲染器的曝光程度:
    this.renderer .toneMappingExposure = 1
    this.container.appendChild(this.renderer.domElement)
  }
  setEnvMap(hdr) {
    new RGBELoader().setPath("./textures/background/").load(hdr + ".hdr", (texture) => {
      //告诉他是一个圆  会虚化
      texture.mapping=THREE.EquirectangularReflectionMapping;
      this.scene.background = texture;
      this.scene.environment = texture;
    
    });
  }
  render() {
    var delta = this.clock.getDelta();
    this.mixer && this.mixer.update(delta)
    this.renderer.render(this.scene, this.camera);
  }
  //请求动画帧函数:
  animate() {
    this.renderer.setAnimationLoop(this.render.bind(this));
  }
  initControls(){
    //创建控制器:
   this.container = new OrbitControls(this.camera,this.renderer.domElement)
    // this.scene.add( this.container)
  }
  //模型解析器:
  setModel(modelName){

  return new Promise((resolve, reject) => {
  //  const loader = new GLTFLoader().setPath("files/gltf/");
  const loader = new GLTFLoader().setPath("files/gltf/");
    loader.load(modelName,(gltf)=>{
      this.model && this.model.removeFromParent()
      this.model=gltf.scene.children[0];
      // if ("bag2.glb" == modelName && !this.panzi) {
      //   this.panzi = gltf.scene.children[5];
      //   // this.scene.add(this.panzi)
      //  //修改摄像头模型的摄像头
      //  this.camera = gltf.cameras[0]
      //  //调用动画:
      //  this.mixer = new THREE.AnimationMixer(gltf.scene.children[1])
      //  this.animateAction = this.mixer.clipAction(gltf.animations[0]);
      //  //   设置动画播放时长:
      //  this.animateAction.setDuration(20).setLoop(THREE.LoopOnce);
      //   //   设置播放完成之后停止:
      //   this.animateAction.clampWhenFinished = true;
      //   //让用户自己操作打造:
      //   // this.animateAction.play();
      //   //设置灯光
      //   this.spotlight1 = gltf.scene.children[3].children[0];
      //   this.spotlight1.intensity = 1;
      //   this.spotlight2 = gltf.scene.children[5].children[0];
      //   this.spotlight2.intensity = 1;
      //   this.spotlight3 = gltf.scene.children[4].children[0];
      //   this.spotlight3.intensity = 1;
        
      //     //把所有都增加进来 包括灯光:
          
      
      // }
  
    // this.light = new THREE.DirectionalLight(0xb5b1c1,1);
    // this.light.intensity = 1.4;
    // this.light.position.set(20,20,20);
    
 
    // this.scene.add(this.light);
      // this.ambientLight = new THREE.AmbientLight();
      // this.scene.add(this.ambientLight);
    gltf.scene.scale.set(1,1,1);
    gltf.scene.position.y =0.2;
    gltf.scene.position.z = 0.8;
    gltf.scene.position.x = 1;
      this.scene.add(gltf.scene);
      
  })
      },
      (e) => {
       
        this.progressFn(e);
      }
    
  )
}
  addMesh(){
    
    this.setModel("apple.glb")
   
    
  }
  //定义自动更新:
  onWindowResize() {
      this.camera.aspect = window.innerWidth / window.innerHeight;
      this.camera.updateProjectionMatrix();//更新投影矩阵
    this.renderer.setSize(window.innerWidth, window.innerHeight);
  }
  // onMouseWheel(e) {
  //   // console.log(this.animateAction);
  //   let timeScale = e.deltaY > 0 ? 1 : -1;
  //   this.animateAction.setEffectiveTimeScale(timeScale);
  //   this.animateAction.paused = false;
  //   this.animateAction.play();
  //   if (this.timeoutid) {
  //     clearTimeout(this.timeoutid);
  //   }
  //   this.timeoutid = setTimeout(() => {
  //     this.animateAction.halt(0.5);
  //   }, 300);
  // }
}

export default Base3d;
